home *** CD-ROM | disk | FTP | other *** search
/ Java Programmer's Toolkit / Java Programmer's Toolkit.iso / solaris2 / jdk / src / java / lang / characte.jav < prev    next >
Encoding:
Text File  |  1995-10-30  |  6.6 KB  |  223 lines

  1. /*
  2.  * W% 95/08/17  
  3.  *
  4.  * Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies. Please refer to the file "copyright.html"
  10.  * for further important copyright and licensing information.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  */
  19.  
  20. package java.lang;
  21.  
  22. /**
  23.  * The Character class provides an object wrapper for Character data values
  24.  * and serves as a place for character-oriented operations.  A wrapper is useful
  25.  * because most of Java's utility classes require the use of objects.  Since characters
  26.  * are not objects in Java, they need to be "wrapped" in a Character instance.
  27.  * @version     1.25, 08/17/95
  28.  * @author    Lee Boynton
  29.  */
  30.  
  31. public final
  32. class Character extends Object {
  33.     /**
  34.      * The minimum radix available for conversion to and from Strings.  
  35.      * The lowest minimum value that a radix can be is 2.
  36.      * @see Integer#toString
  37.      */
  38.     public static final int MIN_RADIX = 2;
  39.  
  40.     /**
  41.      * The maximum radix available for conversion to and from Strings.  The
  42.      * largest maximum value that a radix can have is 36.
  43.      * @see Integer#toString
  44.      */
  45.     public static final int MAX_RADIX = 36;
  46.  
  47.     static char downCase[]; /* case folding translation table upper => lower */
  48.     static char upCase[]; /* case folding translation table lower => upper*/
  49.     static {
  50.         char down[] = new char[256];
  51.         char up[] = new char[256];
  52.         for (int i = 0 ; i < 256 ; i++) {
  53.             down[i] = up[i] = (char) i;
  54.     }
  55.         for (int lower = 'a' ; lower <= 'z' ; lower++) {
  56.             int upper = (lower + ('A' - 'a'));
  57.             up[lower] = (char)upper;
  58.             down[upper] = (char)lower;
  59.     }
  60.  
  61.         for (int lower = 0xE0; lower <= 0xFE; lower++) {
  62.             if (lower != 0xF7) { // multiply and divide
  63.                 int upper = (lower + ('A' - 'a'));
  64.                 up[lower] = (char)upper;
  65.                 down[upper] = (char)lower;
  66.             }
  67.         }
  68.     downCase = down;
  69.     upCase = up;
  70.     }
  71.  
  72.  
  73.     /**
  74.      * Determines if the specified character is ISO-LATIN-1 lower case.
  75.      * @param ch    the character to be tested
  76.      * @return     true if the character is lower case; false otherwise.
  77.      */
  78.     public static boolean isLowerCase(char ch) {
  79.     // its a lower case if it has a different uppercase, or it is a
  80.     // German double-S or Dutch ij.
  81.     return ((ch <= '\u00FF') && 
  82.         ((upCase[ch] != ch) || (ch == '\u00df') || (ch == '\u00ff')));
  83.     }
  84.     
  85.     /**
  86.      * Determines if the specified character is ISO-LATIN-1 upper case.
  87.      * @param ch    the character to be tested
  88.      * @return     true if the character is upper case; false otherwise.
  89.      */
  90.     public static boolean isUpperCase(char ch) {
  91.     // it's upper case if it has a different lower case
  92.         return (ch <= '\u00FF' && downCase[ch] != ch);
  93.     }
  94.  
  95.     /**
  96.      * Determines if the specified character is a ISO-LATIN-1 digit.
  97.      * @param ch    the character to be tested
  98.      * @return     true if this character is a digit; false otherwise.
  99.      */
  100.     public static boolean isDigit(char ch) {
  101.     return (ch >= '0') && (ch <= '9');
  102.     }
  103.  
  104.  
  105.     /**
  106.      * Determines if the specified character is ISO-LATIN-1 white space according to Java.
  107.      * @param ch        the character to be tested
  108.      * @return  true if the character is white space; false otherwise.
  109.      */
  110.     public static boolean isSpace(char ch) {
  111.     switch (ch) {
  112.     case ' ':
  113.     case '\t':
  114.     case '\f': // form feed
  115.     case '\n':
  116.     case '\r':
  117.         return (true);
  118.     }
  119.     return (false);
  120.     }
  121.  
  122.     /**
  123.      * Returns the lower case character value of the specified ISO-LATIN-1 
  124.      * character. Characters that are not upper case letters are returned 
  125.      * unmodified. 
  126.      * @param ch    the character to be converted
  127.      */
  128.     public static char toLowerCase(char ch) {
  129.     return (ch <= '\u00FF') ? downCase[ch] : ch;
  130.     }
  131.  
  132.     /**
  133.      * Returns the upper case character value of the specified ISO-LATIN-1 
  134.      * character.
  135.      * Characters that are not lower case letters are returned unmodified.
  136.      *
  137.      * Note that German ess-zed and latin small letter y diaeresis have no
  138.      * corresponding upper case letters, even though they are lower case.
  139.      * There is a capital y diaeresis, but not in ISO-LATIN-1...
  140.      * @param ch    the character to be converted
  141.      */
  142.     public static char toUpperCase(char ch) {
  143.     return (ch <= '\u00FF') ? upCase[ch] : ch;
  144.     }
  145.  
  146.     /**
  147.      * Returns the numeric value of the character digit using the specified
  148.      * radix. If the character is not a valid digit, it returns -1.
  149.      * @param ch        the character to be converted
  150.      * @param radix     the radix
  151.      */
  152.     public static int digit(char ch, int radix) {
  153.     if (radix >= MIN_RADIX && radix <= MAX_RADIX) {
  154.         if (radix <= 10) {
  155.         char max = (char)('0' + radix - 1);
  156.         if ((ch >= '0') && (ch <= max)) {
  157.             return (ch - '0');
  158.         }
  159.         } else {
  160.         ch = toLowerCase(ch);
  161.         if ((ch >= '0') && (ch <= '9')) {
  162.             return (ch - '0');
  163.         } 
  164.  
  165.         char max = (char)('a' + radix - 11);
  166.         if (ch >= 'a' && ch <= max) {
  167.             return (10 + ch - 'a');
  168.         }
  169.         }
  170.     }
  171.         return -1;
  172.     }
  173.  
  174.     /**
  175.      * Returns the character value for the specified digit in the specified
  176.      * radix. If the digit is not valid in the radix, the 0 character
  177.      * is returned.
  178.      * @param digit    the digit chosen by the character value
  179.      * @param radix     the radix containing the digit
  180.      */
  181.     public static char forDigit(int digit, int radix) {
  182.     if ((digit >= radix) || (digit < 0)) {
  183.         return '\0';
  184.     }
  185.     if ((radix < MIN_RADIX) || (radix > MAX_RADIX)) {
  186.         return '\0';
  187.     }
  188.     if (digit < 10) {
  189.         return (char)('0' + digit);
  190.     } 
  191.     return (char)('a' + digit - 10);
  192.     }
  193.  
  194.     /**
  195.      * The value of the Character.
  196.      */
  197.     private char value;
  198.  
  199.     /**
  200.      * Constructs a Character object with the specified value.
  201.      * @param value value of this Character object
  202.      */
  203.     public Character(char value) {
  204.     this.value = value;
  205.     }
  206.  
  207.     /**
  208.      * Returns the value of this Character object.
  209.      */
  210.     public char charValue() {
  211.     return value;
  212.     }
  213.  
  214.     /**
  215.      * Returns a String object representing this character's value.
  216.      */
  217.     public String toString() {
  218.     char buf[] = {value};
  219.     return String.valueOf(buf);
  220.     }
  221. }
  222.  
  223.